 <template>
  <div class="mod-user">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.fileId"
          clearable
          placeholder="脚本Id"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button
          :disabled="dataListSelections.length <= 0"
          type="danger"
          @click="deleteHandle()"
          >批量删除
        </el-button>
        <el-button v-show="isShow" type="primary" @click="back()"
          >返回
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        label="报告id"
        header-align="center"
        align="center"
        width="80"
        prop="reportId"
      >
      </el-table-column>
      <el-table-column
        label="报告名称"
        header-align="center"
        align="center"
        prop="originName"
      >
        <template slot-scope="scope">
          <router-link
            :to="{
              name: 'test-report-result',
              query: { reportName: scope.row.reportName },
            }"
         
          :style="activation(scope.row.status)"
          >
            {{ scope.row.originName }}
          </router-link>
        </template>
      </el-table-column>
      <el-table-column
        label="脚本ID"
        header-align="center"
        align="center"
        prop="fileId"
      >
      </el-table-column>
      <el-table-column
        label="文件大小"
        header-align="center"
        align="center"
        prop="fileSize"
      >
        <template slot-scope="scope">
          {{ scope.row.fileSize | conver(scope.row.fileSize) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small">创建成功</el-tag>
          <el-tag v-if="scope.row.status === 1" size="small" type="primary"
            >正在生成报告中</el-tag
          >
          <el-tag v-if="scope.row.status === 2" size="small" type="success"
            >报告生成成功</el-tag
          >
          <el-tag v-if="scope.row.status === 3" size="small" type="danger"
            >生成失败</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="addTime"
        header-align="center"
        align="center"
        width="180"
        label="创建时间"
      >
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="修改时间"
        prop="updateTime"
        width="180"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            @click="createReport(scope.row.reportId)"
            >生成报告
          </el-button>
          <el-button
            size="small"
            type="text"
            @click="downloadReport(scope.row.reportId)"
            >下载
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dataForm: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      caseId: "",
      dataListLoading: false,
      dataListSelections: [],
      isShow: false,
      aHref: "",
      isActive: true,
      isLine: false,
    };
  },
  components: {
    // AddOrUpdate
  },
  activated() {
    this.getDataList();
  },
  // 过滤器
  filters: {
    //文件大小处理
    conver(fileSize) {
      var size = "";
      if (fileSize < 0.1 * 1024) {
        //如果小于0.1KB转化成B
        size = fileSize.toFixed(2) + "B";
      } else if (fileSize < 0.1 * 1024 * 1024) {
        //如果小于0.1MB转化成KB
        size = (fileSize / 1024).toFixed(2) + "KB";
      } else if (fileSize < 0.1 * 1024 * 1024 * 1024) {
        //如果小于0.1GB转化成MB
        size = (fileSize / (1024 * 1024)).toFixed(2) + "MB";
      } else {
        //其他转化成GB
        size = (fileSize / (1024 * 1024 * 1024)).toFixed(2) + "GB";
      }
      var sizestr = size + "";
      var len = sizestr.indexOf(".");
      var dec = sizestr.substr(len + 1, 2);
      if (dec == "00") {
        //当小数点后为00时 去掉小数部分
        return sizestr.substring(0, len) + sizestr.substr(len + 3, 2);
      }
      return sizestr;
    },
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/test/stressReports/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          fileId: this.dataForm.fileId,
          order:'asc'
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 删除
    deleteHandle(id) {
      var reportIds = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.reportId;
          });
      this.$confirm(
        `确定对[id=${reportIds.join(",")}]进行[${
          id ? "删除" : "批量删除"
        }]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/test/stressReports/delete"),
            method: "post",
            data: this.$http.adornData(reportIds, false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },

    //生成报告
    createReport(id) {
      let reportId = [id];
      this.$http({
        url: this.$http.adornUrl("/test/stressReports/createReport"),
        method: "post",
        data: this.$http.adornData(reportId, false),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: data.msg,
            type: "success",
            duration: 1500,
            onClose: () => {
              this.getDataList();
            },
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //下载报告
    downloadReport(reportId) {
      this.$http({
        url: this.$http.adornUrl(
          "/test/stressReports/downloadReport/" + reportId
        ),
        method: "get",
        headers: {
          "Content-Type": "application/octet-stream;charset=utf-8",
        },
        responseType: "blob",
      })
        .then((res) => {
          console.log(res.headers);
          let fileName = res.headers["content-disposition"]
            .split(";")[1]
            .split("=")[1];
          console.log(fileName);
          let url = window.URL.createObjectURL(new Blob([res.data]));
          let a = document.createElement("a");
          a.style.display = "none";
          a.href = url;
          a.setAttribute("download", fileName);
          document.body.appendChild(a);
          a.click(); //执行下载
          window.URL.revokeObjectURL(a.href);
          document.body.removeChild(a);
          this.$message({
            message: "下载成功！",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.getDataList();
            },
          });
        })
        .catch((error) => {
          console.log(error);
        });
    },

    back() {
      this.$router.go(-1); //返回上一层
    },

    sizeColor(status) {
      if (status === 2) {
        this.isActive = false;
        this.isLine = true;
      }
    },
  },
  //监听
  watch: {
    $route(now, old) {
      //监控路由变换，控制返回按钮的显示
      if (now.path == "/home/home") {
        this.isShow = false;
      } else {
        // 监听的上一次路由的值,从路由的值中,初始化传的参数,放到监听里面,是为了让页面不停的跳转使用,
        // 不建议放到created中,因为created 只加载使用一次
        console.log('路由的fileId',this.$route.query.fileId);
        this.dataForm.fileId = this.$route.query.fileId
        console.log('路由的fileId',this.dataForm.fileId)
        this.isShow = true;
      }
    },
  },
  created() {
    // this.dataForm.fileId = this.$route.query.fileId
    //设置路由跳转
    // this.$set(this.dataForm, "reportName", this.$route.query.reportName);
    this.getDataList()
  },
  computed: {
    activation() {
      return (status) => {
        // 使用JavaScript闭包，进行传值操作
        console.log("status", status);
        if (status === 2) {
        
          return { color: '#17b3a3' };
        } else {
          return { color: "#606266" };
        }
      };
    },
  },
};
</script>
  <style>
.active {
  color: #606266;
}

.line {
  color: #17b3a3;
}
</style> 